{{ define "index.html" }}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Gocrond Dashboard</title>
    <script src="/js/jquery.js" type="text/javascript"></script>
    <style type="text/css">
        * { font-family:Tahoma, Arial,sans-serif; font-size:12px; line-height:140%; }
        table { width:400px; border:1px #999 solid; border-right: 0; border-bottom: 0; margin-top:15px; }
        tr.head td { height:28px; background-color: #ddd; font-size:bold; font-size: 16px;}
        td { border-right: 1px #999 solid; border-bottom: 1px #999 solid; padding:8px; }
        table .title { background: #f2f2f2; border-bottom:1px #999 solid; font-weight:bold; }
        h1 { font-size: 28px; font-weight: bold; font-style: italic; margin: 0; color: #669; }
        hr { margin: 0; margin-top: 5px; height: 12px; } 
    </style>
</head>

<body>
    <h1>Gocrond Dashboard</h1>
    <hr />
    <p style="margin:12px 0 0 0;padding:0 0 0 3px;text-align: left;">
    <a href="/" title="所有分组">所有分组</a>
    {{ range $v := .groups }}
    |
    <a href="/?group={{$v}}" title="仅查看该分组任务">{{ $v }}</a>
    {{ end }}
    </p>
    {{ if .job_changed }}
    <p style="color:red; margin: 10px 0 0 0; padding:0;text-align:right;">NOTICE：有新增或删除的任务配置项，可<a name="action" href="/?action=reload" title="重载新增删除配置项">点此重新加载</a></p>
    {{ end }}
    {{ if .errstr }}
    <p style="color:red; margin: 10px 0 0 0; padding:0 5px 0 0;text-align:right;">ERROR：配置已变更但无法被加载, {{ .errstr }}</p>
    {{ end }}
    <table style="width:100%" cellspacing="0" cellpadding="4">
    <tr class="head">
        <td width="12%" height="28px">任务名</td>
        <td width="6%">分组</td>
        <td width="18%">描述</td>
        <td width="5%">开启</td>
        <td width="7%">定时规则</td>
        <td width="7%">自动重启</td>
        <td width="8%">最大存活期</td>
        <td width="5%">运行中</td>
        <td width="6%">进程数</td>
        <td width="8%">
            <select name="showtime" style="padding: 4px">
                <option value="last_exec">最后执行时间</option>
                <option value="last_chec">最后检查时间</option>
                <option value="last_load">配置载入时间</option>
            </select>
        </td>
        <td width="5%">状态</td>
        <td width="12%">可选操作</td>
    </tr>
    {{ range $v := .jobs }}
    {{ if $v.Show }}
    <tr name="item">
        <td><a name="job" jobname="{{$v.Name}}"  href="javascript:void(0)" title="点击查看任务配置明细">{{ $v.Name }}</a></td>
        <td><a href="/?group={{$v.Group}}" title="仅查看该组任务">{{ $v.Group }}</a></td>
        <td>{{ $v.Desc }}</td>
        <td>{{ $v.Open }}</td>
        <td>{{ $v.Timer }}</td>
        <td>{{ $v.Restart }}</td>
        <td>{{ $v.Maxlife }} s</td>
        <td>{{ $v.Active }}</td>
        <td>{{ $v.Procnum }}</td>
        <td name="time" exectime="{{$v.Runtime}}" chectime="{{$v.Jumptime}}" loadtime="{{ $v.Loadtime }}">{{ $v.Runtime }}</td>
        <td><span title="{{ $v.Error }}">{{ $v.State }}</span></td>
        <td>
            {{ if $v.Open }}
            <a name="action" href="/?action=close&name={{$v.Name}}" title="关闭任务[{{$v.Name}}]">关闭</a>
            {{ else }}
            <a name="action" href="/?action=open&name={{$v.Name}}" title="开启任务[{{$v.Name}}]">开启</a>
            {{ end }}
            |
            {{ if $v.Active }}
            <a name="action" href="/?action=killproc&name={{$v.Name}}" title="杀死任务[{{$v.Name}}]">结束</a>
            {{ else }}
            <a name="action" href="/?action=runjob&name={{$v.Name}}" title="运行任务[{{$v.Name}}]">执行</a>
            {{ end }}
            |
            <a name="getlog" job="{{$v.Name}}" log="{{$v.Log}}" href="/?action=getlog&name={{$v.Name}}&line=10" title="查看最近10条任务日志">日志</a>
            {{ if $v.Updated }}
            |
            <a name="action" href="/?action=reload&name={{$v.Name}}" title="重载任务[{{$v.Name}}]配置项">重载</a>
            {{ end }}
        </td>
    </tr>
    {{ end }}
    {{ end }}
    </table>

    <hr style="margin-top:12px;margin-bottom:10px;" />
    powered by gocrond v{{.version}} authored by {{.author}}&lt;{{.email}}&gt; last updated in {{.update}}
</body>

</html>
<script>
$(function() {

    var refresh = "{{.refresh}}";
    if (isNaN(refresh)) {
        refresh = 0;
    } else {
        refresh = parseInt(refresh);
    }
    if (refresh > 0) {
        setInterval(() => {
            location.reload();
        }, refresh*1000);
    }

    $('tr[name="item"]').mouseover(function() {
        $(this).find('td').css('background-color', '#dddddd');
    });

    //
    $('tr[name="item"]').mouseout(function() {
        $(this).find('td').css('background-color', '#ffffff');
    });

    var config = {}
    {{ range $v := .jobs }}
    config["{{$v.Name}}"] = {
        "name": "{{$v.Name}}",
        "open": "{{$v.Open}}",
        "command": "{{$v.Command}}",
        "killby": "{{$v.Killby}}",
        "owner": "{{$v.Owner}}",
        "group": "{{$v.Group}}",
        "log": "{{$v.Log}}",
        "timer": "{{$v.Timer}}",
        "restart": "{{$v.Restart}}",
        "maxlife": "{{$v.Maxlife}}",
        "desc": "{{$v.Desc}}"
    };
    {{ end }}

    $('a[name="job"]').click(function() {
        var name = $(this).attr('jobname');
        var result = "{\n";
        result += "name: \"" + config[name].name + "\",\n";
        result += "open: \"" + config[name].open + "\",\n";
        result += "command: \"" + config[name].command + "\",\n";
        result += "killby: \"" + config[name].killby + "\",\n";
        result += "owner: \"" + config[name].owner + "\",\n";
        result += "group: \"" + config[name].group + "\",\n";
        result += "log: \"" + config[name].log + "\",\n";
        result += "timer: \"" + config[name].timer + "\",\n";
        result += "restart: \"" + config[name].restart + "\",\n";
        result += "maxlife: \"" + config[name].maxlife + "\",\n";
        result += "desc: \"" + config[name].desc + "\"\n";
        result += "}\n";
        alert(result);
    });

    $('a[name="getlog"]').click(function() {
        var href = $(this).attr('href');
        window.open(href, "logwindow", "width=800,height=400,channelmode=no,directories=no,status=no,toolbar=no,menubar=no,location=no,resizable=no,top=150,left=150");   
        return false;
    });

    $('select[name="showtime"]').change(function() {
        var type = $(this).val();
        $('td[name="time"]').each(function() {
            if (type == 'last_exec') {
                $(this).html($(this).attr('exectime'));
            } else if (type == 'last_chec') {
                $(this).html($(this).attr('chectime'));
            } else if (type == 'last_load') {
                $(this).html($(this).attr('loadtime'));
            }
        });
    });

    $('a[name="action"]').click(function() {
        var title = $(this).attr('title');
        var href = $(this).attr('href');
        if (!confirm('即将' + title + '，是否继续？')) {
            return false;
        }
        $.getJSON(href, function(data) {
            if (!data.status) {
                alert("操作失败, "+ data.message);
            } else {
                location.reload();
            }
        });
        return false;
    });

});
</script>
{{ end }}